/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD.WBUser;

import csse3004.GD.DA.WBUserDA;
import csse3004.GD.Question;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import csse3004.GD.Poll;

import java.util.ArrayList;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Lan Ye s4223901
 */
public class ViewPollController {

    private WBUserService WBUserService = new WBUserDA();
    protected final Log logger = LogFactory.getLog(getClass());

    /**
     *This function is for getting the whole polls which is open for web user
     *
     **/
    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) {
        ArrayList<Poll> pollList = new ArrayList<Poll>();
        //pollList = new Poll().genTestPoll();//Dummy
        int userID = Integer.parseInt((String) hsr.getSession().getAttribute("userID"));
        pollList = WBUserService.getPolls(userID);
        return new ModelAndView("view", "polls", pollList);
    }

    /**
     *The function is called when user views the opened poll data
     *
     **/
    
     
    @RequestMapping("view")
    public ModelAndView linkedRequest(@RequestParam("pollID") String pollID, HttpServletRequest hsr, HttpServletResponse hsr1) {
        Poll poll = WBUserService.getPoll(Integer.parseInt(pollID));
        //Dummy Data
        //Poll poll = new Poll().genTestPoll().get(Integer.parseInt(hsr.getParameter("pollID")));
        ArrayList<Question> questions = WBUserService.getQuestions(Integer.parseInt(pollID));
        //ArrayList<Question> questions = new Question().genTestQuestion(Integer.parseInt(pollID));
        ModelAndView mnv = new ModelAndView("view", "poll", poll);
        mnv.addObject("questionsall", questions);
        return mnv;
    }
}
